package com.google.guava.eventbus.internal;

import java.util.concurrent.Executor;

/**
 * @author fuc
 * @create 2020-10-07
 */
public class MyEventBus implements Bus {

  private final MyRegistry registry = new MyRegistry();

  private String busName;

  private static final String DEFAULT_BUS_NAME = "default";

  private static final String DEFAULT_TOPIC = "default-topic";

  //  private final MyEventExceptionHandle exceptionHandle;

  private final MyDispatcher dispatcher;

  public MyEventBus() {
    this(DEFAULT_BUS_NAME, null, MyDispatcher.SEQ_EXECUTOR_SERVICE);
  }

  public MyEventBus(String busName) {
    this(busName, null, MyDispatcher.SEQ_EXECUTOR_SERVICE);
  }

  public MyEventBus(String busName, MyEventExceptionHandler exceptionHandler, Executor executor) {
    this.busName = busName;
    //    this.exceptionHandle = exceptionHandle;
    this.dispatcher = MyDispatcher.newDispatcher(exceptionHandler, executor);
  }

  public MyEventBus(MyEventExceptionHandler exceptionHandler) {
    this(DEFAULT_BUS_NAME, exceptionHandler, MyDispatcher.SEQ_EXECUTOR_SERVICE);
  }


  @Override
  public void register(Object subscriber) {
    this.registry.bind(subscriber);
  }

  @Override
  public void unregister(Object subscriber) {
    this.registry.unbind(subscriber);
  }

  @Override
  public void post(Object event) {
    this.post(event, DEFAULT_TOPIC);
  }

  @Override
  public void post(Object event, String topic) {
    this.dispatcher.dispatch(this, registry, event, topic);
  }

  @Override
  public void close() {
    this.dispatcher.close();
  }

  @Override
  public String getBusName() {
    return null;
  }
}
